<!-- 博客嵌入 Mastodon 嘟嘟 -->
<style>
    .header {
        background-image: url('https://cdn.ftls.xyz/images/2023/06/20230820123739.png');
        background-size: cover;
        background-position: center;
        height: 200px;
        position: relative;
        margin-bottom: 70px;
        border-radius: 5px;
        box-shadow: 0 0 10px 1px #2d96bd;
    }

    .avatar {
        width: 100px;
        height: 120px;
        border-radius: 15%;
        box-shadow: 0 0 3px 0.5px rgb(146, 255, 146);
    }

    .toots-container {
        margin: 0 auto;
    }

    .toot {
        margin-bottom: 10px;
    }

    .toot .avatar {
        width: 50px;
        height: 50px;
        border-radius: 50%;
        margin-right: 5px;
    }

    .toot-info {
        display: flex;
        align-items: center;
        margin-bottom: 10px;
    }



    .basic-info {
        position: absolute;
        /* Use absolute positioning */
        bottom: -70px;
        display: flex;
        align-items: center;
        margin-left: 10px;
    }

    .toot-stats {
        display: flex;
        justify-content: flex-end;
        /* 将元素靠右对齐 */
    }

    .toot-stats i {
        margin-right: 3rem;
    }

    .basic-field-status {
        border: 1px solid #2d97bd86;
        border-radius: 30px;
        background-color: rgba(255, 255, 255, 0.05);
    }

    .basic-text {
        margin-top: 50px;
    }

    /* Cat Ear */

    .basic-avatar img {
        position: absolute;
        z-index: 2;
        object-fit: cover;
        width: 100px;
        height: 100px;
        left: calc(50% - 50px);
    }

    .basic-avatar::before,
    .basic-avatar::after {
        background: #ebbcba;
        border: solid 3px #ead9d8;
        box-sizing: border-box;
        content: "";
        display: inline-block;
        height: 50px;
        width: 50px;
        margin-top: -5px;
        /* margin-right: -5px; */
    }

    .basic-avatar::before {
        border-radius: 25% 75% 75%;
        transform: rotate(37.5deg) skew(30deg);
    }

    .basic-avatar::after {
        border-radius: 75% 25% 75% 75%;
        transform: rotate(-37.5deg) skew(-30deg);
    }

    .basic-avatar img {
        position: absolute;
        bottom: 0;
        left: 0;
        right: 0;
        top: 0;
        z-index: 1;
        overflow: hidden;
        object-fit: cover;
    }

    .basic-avatar img:hover {
        position: absolute;
    }

    .basic-avatar {
        height: 120px;
    }

    .basic-avatar:hover::before {
        animation: earwiggleleft 1s infinite;
    }

    .basic-avatar:hover::after {
        animation: earwiggleright 1s infinite;
    }

    /* Cat Ear End */

    /* Media Query for Mobile Devices */
    @media only screen and (max-width: 600px) {
        .header {
            height: 150px;
            padding: 10px;
        }

        .toots-container {
            padding: 10px;
        }

        .toot .avatar {
            width: 40px;
            height: 40px;
        }

        .avatar {
            width: 100px;
            height: 100px;
        }

        .basic-info {
            margin-left: 2px;
        }

        .basic-avatar::before,
        .basic-avatar::after {
            margin-top: -3px;
        }
    }

    /* 430px or 200px hide img and ears */
    @media only screen and (max-width: 430px) {

        .basic-avatar::before,
        .basic-avatar::after {
            display: none;
        }

        .basic-text {
            margin-top: 100px;
            margin-left: -25px;
        }

        .basic-avatar img {
            margin-right: 10px;
            display:none;
        }
    }
</style>

<body>
    <div class="header">
        <div class="basic-info">
            <div class="basic-avatar">
                <img src="https://ftls.xyz/img/avatar.webp" alt="kkbt" class="avatar">
            </div>
            <div class="basic-text">
                <strong>&nbsp;&nbsp;恐咖兵糖</strong> <a href="https://fmb.ftls.xyz/@kkbt">@kkbt</a>&nbsp;
                <span id="basic-field-status" class="basic-field-status"></span>
                <br>&nbsp;&nbsp;微语<span id="basic-statuses-count"></span>&nbsp;
                <a href="https://gotosocial-kkbt-tools-xkzlczgovq.cn-hangzhou.fcapp.run/@kkbt/feed.rss">RSS</a>
            </div>
        </div>
    </div>
    <div id="basic-bio"></div>
    <div id="basic-profile"></div>
    <hr style="margin: 0.5rem 0;">
    <div id="toots-content" class="toots-container">
        <div class="toot" id="toots">
            <!-- show toots -->
        </div>
        <i id="toots-loading" class="fa fa-spinner fa-pulse fa-3x fa-fw" style="display: none;"></i>
        <button id="toots-moreButton" onclick="tootsShowMore()"><a>更多</a></button>
    </div>
</body>

<script type="text/javascript" src="/js/time-fmt.min.js"></script>
<script type="text/javascript" src="https://cdn.ftls.xyz/static/whispers/assets/js/view-image.min.js"></script>
<script>
    let maxId = null; // 初始值为 null，表示第一页
    const tootsDiv = document.getElementById('toots');
    const tootsMoreButton = document.getElementById('toots-moreButton');
    const tootsLoading = document.getElementById('toots-loading');

    // 获取 Mastodon 用户公开Toots 限制条数 默认5 排除回复 toot
    async function getPublicToots() {
        const queryParams = maxId ? ("?limit={{ .Get 2 | default 5 }}&max_id=" + maxId) : "?limit={{ .Get 2 | default 5 }}";
        const response = await fetch("{{ .Get 0 }}/api/v1/accounts/{{ .Get 1 }}/statuses" + queryParams + "&exclude_replies=true");
        const toots = await response.json();
        return toots;
    }

    // 解析ULID
    function parseULID(ulid) {
        const base32Chars = '0123456789ABCDEFGHJKMNPQRSTVWXYZ';
        const timestamp = parseInt(ulid.slice(0, 10).split('').map(char => base32Chars.indexOf(char)).map(index => index.toString(2).padStart(5, '0')).join(''), 2);
        const randomPart = ulid.slice(10);

        return {
            timestamp: new Date(timestamp),
            randomPart: randomPart
        };
    }

    // 将Toots显示在页面上
    async function displayToots() {
        try {
            tootsLoading.style.display = "block";
            tootsMoreButton.style.display = 'none';
            const toots = await getPublicToots();
            if (toots && toots.length > 0) {
                displayBioProfile(toots[0]);
                toots.forEach(toot => {
                    // console.log(parseULID(toot.id)); // 解析 ULID
                    const tootDiv = document.createElement("div");

                    tootDiv.classList.add("toot");

                    const tootInfoDiv = document.createElement("div");
                    tootInfoDiv.classList.add("toot-info");

                    const tootAvatar = document.createElement("div");
                    tootAvatar.classList.add("toot-avatar");

                    const profileImage = document.createElement("img");
                    profileImage.src = "https://ftls.xyz/img/avatar.webp";
                    profileImage.classList.add("avatar");
                    profileImage.alt = toot.account.display_name;

                    const tootProfileDiv = document.createElement("div");
                    tootProfileDiv.innerHTML = `<strong>${toot.account.display_name}</strong> <a href="${toot.url}">@${toot.account.acct}</a><br><small>${formatTime(toot.created_at)}</small>`;

                    tootAvatar.appendChild(profileImage);
                    tootInfoDiv.appendChild(profileImage);
                    tootInfoDiv.appendChild(tootProfileDiv);

                    const contentDiv = document.createElement("div");
                    contentDiv.classList.add("toot-content");
                    contentDiv.innerHTML = toot.content;

                    // media  loading="lazy"
                    for (let i = 0; i < toot.media_attachments.length; i++) {
                        const media = toot.media_attachments[i];
                        contentDiv.innerHTML += `<img src="${media.url}">`;
                    }

                    const tootStats = document.createElement("a");
                    tootStats.href = toot.url;
                    tootStats.className = "toot-stats";
                    if (toot.replies_count + toot.favourites_count + toot.reblogs_count != 0) {
                        tootStats.innerHTML += `<i class="fa fa-reply-all">&nbsp;${toot.replies_count}</i>`;
                        tootStats.innerHTML += `<i class="fa fa-star">&nbsp;${toot.favourites_count}</i>`;
                        tootStats.innerHTML += `<i class="fa fa-retweet">&nbsp;${toot.reblogs_count}</i>`;
                    }

                    const hr = document.createElement("hr");
                    hr.style = "margin: 0.5rem 0;"

                    tootDiv.appendChild(tootInfoDiv);
                    tootDiv.appendChild(contentDiv);
                    tootDiv.appendChild(tootStats);
                    tootDiv.appendChild(hr);

                    tootsDiv.appendChild(tootDiv);
                    maxId = toot.id; // 更新最大 ID
                });
                tootsMoreButton.style.display = 'block';
            } else {
                tootsMoreButton.style.display = 'none';
            }
        } catch (error) {
            console.error('获取 Toots 时出错：', error);
            tootsDiv.innerHTML += error.message;
        }
        tootsLoading.style.display = "none";
    }

    function tootsShowMore() {
        displayToots();
    }

    function displayBioProfile(statuse) {
        document.getElementById("basic-statuses-count").innerText = `共计 ${statuse.account.statuses_count} 条`;
        document.getElementById("basic-bio").innerHTML = statuse.account.note;
        let fieldsHtml = "";
        statuse.account.fields.forEach(field => {
            fieldsHtml += `<strong>${field.name}</strong> <span>${field.value}</span><br>`;
            if (field.name === "Status") {
                document.getElementById("basic-field-status").innerHTML = `&nbsp;${field.value}&nbsp;`;
            }
        });
        document.getElementById("basic-profile").innerHTML = fieldsHtml;
    }

    displayToots();
    // 页面加载时调用显示Toots函数
    // window.onload = displayToots;
    window.ViewImage && ViewImage.init('.toot-content img');
</script>